package ext.tianma.part.imported;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.inf.container.WTContainer;
import wt.lifecycle.State;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.query.QueryException;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.session.SessionServerHelper;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.vc.views.View;
import wt.vc.views.ViewException;
import wt.vc.views.ViewHelper;
import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;
import ext.generic.part.CusIBAUtil;
import ext.tianma.part.forms.TMTypeUtil;

public class CusImportParts implements RemoteAccess {
	private static final String CLASSNAME = CusImportParts.class.getName() ;
	
	private static final String NEW_PRODUCT = "NewProduct";
	//量产发布
	private static final String MASS_PRODUCTION_RELEASE = "MassProductionRelease";	
	//废弃
	private static final String MASS_OBSOLESCENCE = "OBSOLESCENCE";
	//废弃
	private static final String OBSOLESCENCE = "wt.lifecycle.State.OBSOLESCENCE";	
	private static final String PART_NEW_PRODUCT = "wt.lifecycle.State.NewProduct";
	private static final String PART_MASS_PRODUCTION_RELEASE = "wt.lifecycle.State.MassProductionRelease";
	
	/**
	 * 导入Excel文件中的物料数据
	 * 
	 * @param excelFile
	 */
	public static void importParts(String excelFile){
		System.out.println(">>>>> CusImportParts.importParts(String excelFile)") ;
		
		//获取Excel文件的第一个sheet页
		Sheet sheet = ImportedExcelUtil.getFirstSheet( excelFile ) ;
		
		boolean flag = SessionServerHelper.manager.setAccessEnforced(false);
		try{
			importParts( sheet ) ;
		}finally{
			SessionServerHelper.manager.setAccessEnforced( flag );
		}
	}

	/**
	 * 导入指定Sheet中的物料数据
	 * 
	 * @param sheet
	 * @return
	 */
	public static StringBuffer importParts( Sheet sheet ){
		System.out.println(">>>>> CusImportParts.importParts( Sheet sheet )") ;
		
		StringBuffer logInfo = new StringBuffer("") ;
		
		if( sheet == null ){
			System.out.println( "Debug   sheet...null" );
			return logInfo ;
		}
		
		//获取Sheet中指定的物料库名称（产品库或者存储库）
		String libraryName = getLibrary(sheet);
		System.out.println( "Debug  libraryName:"+libraryName );
		WTContainer container = null ;
		
		try {
			//获取需要导入的物料库
			container = getContainer( libraryName ) ;
			System.out.println( "Debug   container:"+container );
			if( container != null ){
				//获取表头内部值
				Map<Integer,String> internalNameMap = getColumnInternalNameMap( sheet ) ;
				//获取表头显示名称
				Map<Integer,String> displayNameMap = getColumnDisplayNameMap( sheet ) ;
				
				//遍历行数据，每行对应一个料号的物料
				Iterator<Row> rowIte = sheet.rowIterator() ;
				while( rowIte.hasNext() ){
					Row row = rowIte.next() ;
					
					if( row != null ){
						int rowIndex = row.getRowNum() ;
						
						int rowNum = rowIndex + 1 ;
						System.out.println(">>>>> Start Process Row " + rowNum ) ;
						
						if( rowIndex >= ImportConstants.DATA_START_ROW_INDEX ){
							
							//按行导入物料
							importParts( row , container ,internalNameMap , displayNameMap , logInfo ) ;
						}
						
						System.out.println(">>>>> End Process Row " + rowNum ) ;
					}
				}
			}
		} catch (QueryException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} finally{
			System.out.println( logInfo ) ;
		}

		return logInfo ;
	}
	
	/**
	 * 按行导入物料的数据，每行物料对应一个料号的零部件
	 * 
	 * @param row Excel文件的行
	 * @param container 导入物料的库 
	 * @param internalNameMap
	 * @param displayNameMap
	 * @param logInfo
	 * 
	 * @throws WTException
	 * @throws RemoteException
	 */
	private static void importParts( Row row , WTContainer container , Map<Integer,String> internalNameMap , Map<Integer,String> displayNameMap , StringBuffer logInfo ) throws WTException, RemoteException {
		if( row ==null || container == null ){
			return ;
		}
		
		System.out.println(">>>>> CusImportParts.importParts( Row row...") ;
		
		//获取料号
		String partNumber = ImportedExcelUtil.getCellValueByIndex(row, ImportConstants.PART_NUMBER_COLUMN_INDEX, true) ;
		
		//根据物料生效地单元格的值，拆分出物料视图
		Integer partNumberEffectiveSiteIndex = getIndex( internalNameMap , ImportConstants.PART_NUMBER_EFFECTIVE_SITE );
		String partNumberEffectiveSite = ImportedExcelUtil.getCellValueByIndex(row , partNumberEffectiveSiteIndex , true) ;
		
		System.out.println(">>>>> partNumber = " + partNumber + " , partNumberEffectiveSite = " + partNumberEffectiveSite) ;
		
		if( partNumberEffectiveSite == null || partNumberEffectiveSite.equals("")){
			ImportedLogUtil.addErrorpartEffectiveSiteLog( row.getRowNum() , logInfo ) ;

			return ;
		}
		
		if( ! partNumber.equals("") ){
			WTPart dPart = CoreUtil.getWTPartByNumberAndView( partNumber , ImportConstants.D_VIEW ) ;
			
			//如果没有Design视图的零部件，则首先创建一个Design视图的零部件
			if( dPart == null ){
				dPart = createDesignPart( row , container , internalNameMap , displayNameMap , logInfo ) ;
				if( dPart == null ){
					System.out.println( "Debug   createDesignPart....dPart...Value..null" );
				}
				dPart = CoreUtil.getWTPartByNumberAndView( partNumber , ImportConstants.D_VIEW ) ;
				if( dPart == null ){
					System.out.println( "Debug   getWTPartByNumberAndView....dPart...Value..null" );
				}
			}

			//按照物料生效地属性，基于Design视图，创建多个视图版本
			if( dPart != null ){
				if( !TMTypeUtil.isTwoLevelMaterial( dPart ) ){
					System.out.println( "Debug   dPart...view:"+dPart.getViewName() );
					newViewPartByDPart(row , dPart, partNumberEffectiveSite, internalNameMap , logInfo);
				}else{
					System.out.println( dPart.getDisplayIdentifier()+"为二级物料类型，导入时不新建视图！" );
				}				
			}		
		}
	}

	/**
	 * 创建Design视图版本的零部件
	 * 
	 * @param row Excel的行
	 * @param container 新建零部件的库
	 * @param internalNameMap
	 * @param displayNameMap
	 * @param logInfo
	 * 
	 * @return WTPart
	 * 
	 * @throws WTException
	 */
	private static WTPart createDesignPart( Row row , WTContainer container , Map<Integer,String> internalNameMap , Map<Integer,String> displayNameMap , StringBuffer logInfo ) throws WTException{
		System.out.println(">>>>> CusImportParts.createDesignPart") ;
		WTPart part = null ;
		
		if(row == null){
			return part ;
		}
		
		try {
			logInfo.append("Create New Design Part : ") ;
			
			//新建零部件
			part = WTPart.newWTPart() ;
			
			//设置零部件视图为Design
			ImportedPartUtil.setView( part, ImportConstants.D_VIEW , logInfo );
			
			//设置零部件的库
			part.setContainer( container ) ;
			
			//保存零部件的其他属性值
			part = saveDesignPartAttributes( row , part , internalNameMap,logInfo );
			
			logInfo.append( ImportedPartUtil.getPartInfo(part)) ;
			logInfo.append("\n") ;
		} catch (WTException e) {
			e.printStackTrace();
		} catch (WTPropertyVetoException e) {
			e.printStackTrace();
		}
		
		return part ;
	}
	
	/**
	 * 按照物料生效地属性，基于Design视图，创建多个视图版本
	 * 
	 * @param row Excel文件的行
	 * @param dPart Design视图的零部件
	 * @param partNumberEffectiveSite Excel表格中的物料生效地
	 * @param internalNameMap
	 * @param logInfo
	 * @throws WTException
	 * @throws RemoteException
	 */
	private static void newViewPartByDPart(Row row, WTPart dPart,String partNumberEffectiveSite , Map<Integer, String> internalNameMap, StringBuffer logInfo) throws WTException, RemoteException {
		if(partNumberEffectiveSite == null){
			return ;
		}

		//获取当前Design视图的物料生效地属性
		String currentEffectiveSite = ( String ) IBAUtil.getIBAValue( dPart , ImportConstants.PART_NUMBER_EFFECTIVE_SITE ) ;
		if(currentEffectiveSite == null){
			currentEffectiveSite = "" ;
		}
		
		currentEffectiveSite = currentEffectiveSite.trim() ;
		
		//按逗号分隔物料生效地，拆分出各个生效地的视图
		String[] effectiveSite = partNumberEffectiveSite.split(",") ;
		
		if( effectiveSite != null ){
			for( int i=0 ; i < effectiveSite.length ; i++ ){
				
				//判断是否可以根据effectiveSite[i]的值新建视图版本
				if( canCreateNewViewPart(dPart, effectiveSite[i]) ){
					
					//新建视图版本
					newPartView( row , dPart , effectiveSite[i] , internalNameMap , logInfo );
					
					//更新设计视图的料号生效地IBA属性值
					if( ! currentEffectiveSite.contains( effectiveSite[i] ) ){
						if( currentEffectiveSite.equals("")){
							currentEffectiveSite = effectiveSite[i] ; 
						}else{
							currentEffectiveSite = currentEffectiveSite + "," + effectiveSite[i] ;
						}
					}
				}
			}
			
			//将新的料号生效地IBA属性值更新到设计视图的零部件对象上
			dPart = (WTPart) CoreUtil.checkoutObject( dPart ) ;					
			CusIBAUtil.forceSetEnumIBAValue( dPart , ImportConstants.PART_NUMBER_EFFECTIVE_SITE , currentEffectiveSite ) ;				
			dPart = (WTPart) CoreUtil.checkinObject(dPart, "客制化导入零部件，新建视图版本") ;
		}
	}

	/**
	 * 判断导入零部件是，是否可以新建指定视图版本的零部件
	 * 
	 * @param dPart 设计视图的零部件
	 * @param newEffectiveSite 新视图版本的零部件
	 * 
	 * @return boolean
	 * 
	 * @throws WTException
	 */
	private static boolean canCreateNewViewPart(WTPart dPart , String newEffectiveSite ) {
		//设计视图零部件为null，或者新视图无效，则返回false
		if( dPart == null || newEffectiveSite == null ){
			return false ;
		}
		
		newEffectiveSite = newEffectiveSite.trim() ;
		
		//新视图无效，则返回false
		if( newEffectiveSite.equals("") ){
			return false ;
		}
		
		//如果系统中不存在名称为newEffectiveSite的零部件视图，则返回false
		View  view = null;
		try {
			view = ViewHelper.service.getView( newEffectiveSite );
			
			if( view != null ){
				
				//如果系统中，此零部件已经存在一个与新视图newEffectiveSite相同的视图版本的零部件，则返回false
				WTPart aPart = CoreUtil.getWTPartByMasterAndView( (WTPartMaster)dPart.getMaster() ,newEffectiveSite ) ;
				if( aPart != null ){	
					String aPartViewName = aPart.getViewName() ;
					if( aPartViewName.equals( newEffectiveSite ) ){
						return false ;
					}
				}
			}			
		} catch (ViewException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		
		if( view == null ){
			System.out.println("Error Info : Can't get the Part View by view name : " + newEffectiveSite ) ;
			return false ;
		}
	
		return true ;
	}
	
	/**
	 * 新建零部件视图版本
	 * 
	 * @param row Excel文件的行
	 * @param part 
	 * @param newView
	 * @param internalNameMap
	 * @param logInfo
	 * @throws WTException
	 */
	private static void newPartView(Row row , WTPart dPart , String newView ,Map<Integer,String> internalNameMap , StringBuffer logInfo ) throws WTException{
		logInfo.append("New Part View Version : ") ;
		
		if(row == null){
			return ;
		}
		
		//如果dPart为null，则不能进行新建视图版本操作
		if(dPart == null){
			return ;
		}else{
			//如果dPart的视图不是Design，则不能进行新建视图版本操作
			String dPartViewName = dPart.getViewName() ;
			if( ! ImportConstants.D_VIEW.equals( dPartViewName )){
				return ;
			}
			
			//如果dPart的视图不是新品和量产发布,废弃，则不能进行新建视图版本操作
			State dPartState = dPart.getLifeCycleState();
			String state = dPartState.getStringValue() ;
			System.out.println("Debug   dPartState:"+state);
			if( ( ! state.equals( PART_NEW_PRODUCT ) ) && ( ! state.equals( PART_MASS_PRODUCTION_RELEASE ) ) && ( ! state.equals( OBSOLESCENCE ) ) ) {
				logInfo.append( "零部件" );
				logInfo.append( dPart.getNumber() );
				logInfo.append("设计视图状态不对，无法生成新的视图版本！");
				
				return ;
			}
		}
		
		//新建视图版本
		WTPart newViewPart = ImportedPartUtil.tansitView( dPart , newView) ;
		System.out.println( "Debug   newViewPart:"+newViewPart.getViewName() );
		CusIBAUtil.forceSetEnumIBAValue( newViewPart , ImportConstants.PART_NUMBER_EFFECTIVE_SITE , newView ) ;
		
		if( newViewPart != null ){
			saveNewViewPartAttributes (row , newViewPart , internalNameMap, logInfo) ;
		}
	}
	
	/**
	 * 保存新建视图版本零部件的属性值
	 * 
	 * @param row
	 * @param part
	 * @param internalNameMap
	 * @param logInfo
	 * @return
	 * @throws WTException
	 */
	private static WTPart saveNewViewPartAttributes (Row row , WTPart part , Map<Integer, String> internalNameMap, StringBuffer logInfo)throws WTException {
		Hashtable<String,Object> ibaTable = new Hashtable<String,Object>() ;
		
		Iterator<Cell> cellIte = row.cellIterator() ;
		
		String newViewPartState = "";
		
		while( cellIte.hasNext() ){
			Cell cell = cellIte.next() ;
			
			int cellColumnIndex = cell.getColumnIndex() ;

			if( cellColumnIndex != ImportConstants.PART_STATE_COLUMN_INDEX ){
				String attributeId = internalNameMap.get( cellColumnIndex ) ;
				String attributeValue = ImportedExcelUtil.getCellValueAsString(cell) ;
				
				logInfo.append(">>>>> ImportedPartUtil.setPartAttribute Start......\n") ;
				
				System.out.println("Cell column Index : " + cellColumnIndex ) ;
				
				ImportedPartUtil.setPartAttribute( part , attributeId , attributeValue , ibaTable , logInfo) ;
				
				logInfo.append(">>>>> ImportedPartUtil.setPartAttribute End......\n") ;
			}else{
				newViewPartState = ImportedExcelUtil.getCellValueAsString(cell) ;
				
			}
		}
		System.out.println( "saveNewViewPartAttributes...newViewPartState="+newViewPartState );
		//新视图版本状态与Desgin视图版本状态相同
		if( newViewPartState.equals( MASS_OBSOLESCENCE ) ){
			ImportedPartUtil.setState(part, MASS_OBSOLESCENCE, logInfo) ;
		}else{
			ImportedPartUtil.setState(part, MASS_PRODUCTION_RELEASE, logInfo) ;
		}
		//新建视图版本的时候，已经设置过物料生效地了
		//另外，这里的值可能包含多个料号生效地的值，对于单个产地，这是错误的值
		if( ibaTable.containsKey( ImportConstants.PART_NUMBER_EFFECTIVE_SITE ) ){
			ibaTable.remove( ImportConstants.PART_NUMBER_EFFECTIVE_SITE ) ;
		}
		
		//设置其他IBA属性值
		IBAUtil.forceSetIBAValues( part , ibaTable );
		
		logInfo.append(">>>>> SetIBAValues End......\n") ;
		
		return part;
	}
	
	/**
	 * 保存设置视图版本零部件的属性值
	 * 
	 * @param row
	 * @param part
	 * @param internalNameMap
	 * @param logInfo
	 * @return
	 * @throws WTException
	 */
	private static WTPart saveDesignPartAttributes (Row row , WTPart part , Map<Integer, String> internalNameMap, StringBuffer logInfo)throws WTException {
		System.out.println(">>>>> CusImportParts.savePartAttributes (Row row") ;
		
		Hashtable<String,Object> ibaTable = new Hashtable<String,Object>() ;
		
		Iterator<Cell> cellIte = row.cellIterator() ;
		
		String partState = "";
		
		while( cellIte.hasNext() ){
			Cell cell = cellIte.next() ;
			
			int cellColumnIndex = cell.getColumnIndex() ;

			String attributeId = internalNameMap.get( cellColumnIndex ) ;
			String attributeValue = ImportedExcelUtil.getCellValueAsString(cell,true) ;
			
			if( attributeId != null ){
				if( attributeId.trim().equals( "State" ) ){
					partState = attributeValue ;
				}
				
				logInfo.append(">>>>> ImportedPartUtil.setPartAttribute Start......\n") ;
				
				ImportedPartUtil.setPartAttribute( part , attributeId , attributeValue , ibaTable , logInfo) ;
				
				logInfo.append(">>>>> ImportedPartUtil.setPartAttribute End......\n") ;
			}	
		}
		
		//保存设计视图的零部件
		part = (WTPart) PersistenceHelper.manager.store( part );
		
		logInfo.append(">>>>> PersistenceHelper.manager.store End......\n") ;
		
		//修改前多有零部件状态为“量产发布”，目前是如果是“废弃”状态则设置为“废弃”，其他状态都设置为“量产发布”
		if( partState.equals( MASS_OBSOLESCENCE ) ){
			ImportedPartUtil.setState(part, MASS_OBSOLESCENCE, logInfo) ;
		}else{
			ImportedPartUtil.setState(part, MASS_PRODUCTION_RELEASE, logInfo) ;
		}
		
		//单独处理多枚举IBA属性赋值 2013-11-12
		if( ibaTable.containsKey( ImportConstants.PART_NUMBER_EFFECTIVE_SITE ) ){
			String ibaValue = (String)ibaTable.get( ImportConstants.PART_NUMBER_EFFECTIVE_SITE );
			
			CusIBAUtil.forceSetEnumIBAValue(part, ImportConstants.PART_NUMBER_EFFECTIVE_SITE , ibaValue);
			
			ibaTable.remove( ImportConstants.PART_NUMBER_EFFECTIVE_SITE );				
		}
		
		//设置其他IBA属性的值
		setAllIBAValues(part, ibaTable );
		
		logInfo.append(">>>>> SetIBAValues End......\n") ;
		
		return part;
	}
	
	/**
	 * 设置IBA属性
	 * @param part
	 * @param ibaTable
	 * @throws WTException
	 */
	private static void setAllIBAValues(WTPart part, Hashtable<String, Object> ibaTable) throws WTException {
		if( part != null && ibaTable != null ){			
			Enumeration<String> key =  ibaTable.keys();
			System.out.println( " setAllIBAValues... part="+part.getDisplayIdentity() );
			if( key != null ){
				
				while (key.hasMoreElements()) {
					String ibaName = key.nextElement();
					
					if( ibaName != null ){					
						Object ibaValue = ibaTable.get( ibaName );
						System.out.println( "Debug   setAllIBAValues...ibaValue="+ibaValue +"  ibaName="+ibaName );
						if( ibaValue != null ){				
							IBAUtil.forceSetIBAValue(part, ibaName, ibaValue);
						}
					}
				}
			}
		}
	}

	/**
	 * 从Excel表头中解析零部件属性的显示名称
	 * 
	 * @param sheet
	 * @return
	 */
	private static Map<Integer,String> getColumnDisplayNameMap( Sheet sheet ) {
		Map<Integer,String> columnNameMap = new HashMap<Integer,String>() ;
		
		if( sheet != null ){
			Map<Integer,String> columnInternalNameMap = getColumnInternalNameMap( sheet ) ;
			
			//ImportConstants.COLUMN_DISPLAY_NAME_ROW_INDEX为零部件属性的显示名称行的行号
			Row columnDisplayRow = sheet.getRow( ImportConstants.COLUMN_DISPLAY_NAME_ROW_INDEX ) ;
			
			if( columnDisplayRow != null ){
				Iterator<Cell> cellIte = columnDisplayRow.cellIterator() ;
				
				while( cellIte.hasNext() ){
					Cell cell = cellIte.next() ;
					
					if( cell != null ){
						int cellColumnIndex = cell.getColumnIndex() ;
						String cellValue = ImportedExcelUtil.getCellValueAsString(cell);
						
						if( cellValue == null || cellValue.equals("")){
							if( columnInternalNameMap.containsKey( cellColumnIndex )){
								
								columnNameMap.put( cellColumnIndex , columnInternalNameMap.get(cellColumnIndex) ) ;
								
							}else{
								columnNameMap.put( cellColumnIndex , cellValue ) ;
							}
						}else{
							columnNameMap.put( cellColumnIndex , cellValue ) ;
						}
					}
				}
			}
		}
		
		return columnNameMap ;
	}
	
	/**
	 * 从Excel表头中解析零部件属性的内部名称
	 * 
	 * @param sheet
	 * @return
	 */
	private static Map<Integer,String> getColumnInternalNameMap( Sheet sheet ) {
		Map<Integer,String> columnNameMap = new HashMap<Integer,String>() ;
		
		if( sheet != null ){
			//ImportConstants.COLUMN_ITERNAL_NAME_ROW_INDEX为零部件属性的内部名称行的行号
			Row columnRow = sheet.getRow( ImportConstants.COLUMN_ITERNAL_NAME_ROW_INDEX ) ;
			
			if( columnRow != null ){
				Iterator<Cell> cellIte = columnRow.cellIterator() ;
				
				while( cellIte.hasNext() ){
					Cell cell = cellIte.next() ;
					
					if( cell != null ){
						int cellColumnIndex = cell.getColumnIndex() ;
						String cellValue = ImportedExcelUtil.getCellValueAsString(cell);
						
						if( cellValue != null  ){
							cellValue = cellValue.trim() ;
							
							if( ! cellValue.isEmpty()){
								columnNameMap.put( cellColumnIndex , cellValue ) ;
							}
						}
					}
				}
			}
		}
		
		return columnNameMap ;
	}
	
	/**
	 * 根据value的值，从Map中获取对应的key值
	 * 
	 * @param NameMap
	 * @param attName
	 * @return
	 */
	private static Integer getIndex( Map<Integer,String> NameMap , String attName){
		Integer index = 0 ;
		
		Iterator<Integer> keyIte = NameMap.keySet().iterator() ;
		while( keyIte.hasNext() ){
			index = keyIte.next() ;
			String value = NameMap.get(index) ;
			
			if( value != null && value.equals( attName )){
				break ;
			}
		}
		
		return index ;
	}
	
	/**
	 * 根据库的名称，查询库
	 * 
	 * @param s
	 * @return
	 * @throws wt.query.QueryException
	 * @throws wt.util.WTException
	 */
	public static WTContainer getContainer(String s) throws wt.query.QueryException, wt.util.WTException {
		QuerySpec queryspec = new QuerySpec(WTContainer.class);
		queryspec.appendSearchCondition(new SearchCondition(WTContainer.class, WTContainer.NAME, SearchCondition.EQUAL , s));
		QueryResult queryresult = PersistenceHelper.manager.find(queryspec);
		WTContainer wtcontainer = null;
		while (queryresult.hasMoreElements()) {
			Object obj = queryresult.nextElement();
			if (obj instanceof WTContainer)
				wtcontainer = (WTContainer) obj;
		}
		return wtcontainer;

	}
	
	/**
	 * 获取需要导入的库的名称
	 * 
	 * 库名存放在Sheet页的第2行第1列，对应的Row Index = 1, Column Index = 0 ;
	 * 
	 * @param sheet
	 * @return
	 */
	private static String getLibrary( Sheet sheet ){
		String library = "";
		
		if( sheet != null ){
			Row row = sheet.getRow( ImportConstants.LIBRARY_ROW_INDEX ) ;
			
			if( row != null ){
				Cell cell = row.getCell( ImportConstants.LIBRARY_COLUMN_INDEX ) ;
				
				if( cell != null ){
					library = ImportedExcelUtil.getCellValueAsString(cell);
				}
			}
		}
		
		return library;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if( args != null && args[0] != null ){
			if ( ! RemoteMethodServer.ServerFlag ) {
				System.out.println( "Debug   RemoteMethodServer..." );
				String method = "importParts";			
			
				try {
					RemoteMethodServer.getDefault().invoke(method, CLASSNAME, null,
							new Class[] { String.class }, new Object[] { args[0] });
					System.out.println( "Debug  end  RemoteMethodServer..." );
				} catch (RemoteException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}else{
				System.out.println( "Debug   importParts..." );
				importParts( args[0] ) ;
				System.out.println( "Debug   end  importParts..." );
			}
		}
	}
}
